/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.event;

import com.jb.web.application.Command;
import com.jb.web.bean.NonDeletable;
import com.jb.web.bean.Page;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;
import com.jb.web.session.Session;
import com.jb.web.session.SessionUser;

import com.jb.faces.component.ActionSource;


public class CommandManager implements UIEventHandler {
    public void handleEvent(WebContext pCtx, UIEvent event) {
        Page page = ((Page) pCtx.getRespondTree());
        Session session = pCtx.getSession();
        String action = event.getParameter("action");
        UIComponentImpl comp = (UIComponentImpl) event.getSource();

        if ("com_jb_design_move".equals(action)) {
            String moveToId = event.getParameter("moveToId");
            UIComponentImpl moveToComp = (UIComponentImpl) page.getDescendantByClientId(moveToId);

            if (moveToComp != null) {
                UIComponentImpl item = (UIComponentImpl) session.getSelected();
                 
                if(session.getClipboard().copyComponent(pCtx, moveToComp, item))
                  item.getParent().getChildren().remove(item);
            }
        }

        if (comp instanceof ActionSource) {
            String command = ((Command) comp).getCommand();
            SessionUser user = pCtx.getSession().getUser();

            //  System.out.println("Command "+command);
            if ("com_jb_design_designMode".equals(command)) {
                if (page.getCreatedBy().equals(user.getUserName())) {
                    page.setDesignable(!page.isDesignable());
                } else {
                    pCtx.addErrorMessage(this, "Not sufficient privilege");
                }
            } else if ("com_jb_design_togglePageDD".equals(command)) {
                session.setDraggable(!session.isDraggable());
            } else if ("com_jb_design_viewMB".equals(command)) {
                session.setShowManagedBean(!session.isShowManagedBean());
            } else if ("com_jb_design_chinese".equals(command)) {
                user.setLanguage("cn");
                session.updateLocale();
            } else if ("com_jb_design_english".equals(command)) {
                user.setLanguage("en");
                session.updateLocale();
            } else if ("com_jb_design_logout".equals(command)) {
                session.logout();
            } else if ("com_jb_design_paste".equals(command)) {
                session.getClipboard().paste(pCtx, (UIComponentImpl) session.getSelected());
            } else if ("com_jb_design_cut".equals(command)) { //can not cut non deleteables

                UIComponentImpl item = (UIComponentImpl) session.getSelected();

                if ((item != null) && !(item instanceof NonDeletable) && !item.isFacet()&&item.isDeletable()) {
                    session.getClipboard().cut(pCtx, item);
                }
            } else if ("com_jb_design_copy".equals(command)) {
                session.getClipboard().copy(pCtx, (UIComponentImpl) session.getSelected());
            } else if ("com_jb_design_delete".equals(command)) {
                UIComponentImpl item = (UIComponentImpl) session.getSelected();

                if ((item != null) && !(item instanceof NonDeletable) && !item.isFacet()&&item.isDeletable()) {
                    if (!(item instanceof Page)) {
                        page = item.getPage();
                        item.delete(pCtx);
                        item.getParent().getChildren().remove(item);
                        session.setSelected(null);

                        if (page != null) {
                            page.save(pCtx);
                        }
                    } else {
                        pCtx.addErrorMessage(this, "Can not delete page!");
                        pCtx.renderResponse();
                    }
                }
            }
        }
    }
}
